#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define LEN sizeof(struct students)

// This is a cpp program which test link.



struct students
{
	int num;
	char name[20];
	char sex;
	int score;
	struct students *next;
};

/*void input(struct students *p1)
{
	puts("Num:");
	scanf("%d",&p1->num);
	puts("Name:");
	scanf("%s",p1->name);
	puts("Sex,M or F:");
	scanf("%c",&p1->sex);
	puts("Score:");
	scanf("%d",&p1->score);
} */

struct students *inputdata(void)
{
	struct students *head,*p1,*p2;
	int n=0;
	//char start;
	p1=p2=(struct students *)malloc(LEN);
	puts("Input num,name,sex,score:\n");
	scanf("%d,%s,%c,%d",&p1->num,p1->name,&p1->sex,&p1->score);
	head=NULL;
	while(p1->num!=0)
	{   n++;
		if(n==1) head=p1;
		else p2->next=p1;
		p2=p1;
		//start=getchar();
		p1=(struct students *)malloc(LEN);
		//printf("Do want input more [Y/N]?\n");
		//scanf("%c",&start);
		//if(start=='n' || start=='N') break;
		puts("Input num,name,sex,score:\n");
		scanf("%d,%s,%c,%d",&p1->num,p1->name,&p1->sex,&p1->score);
	}
	p2->next=NULL;
	return head;
}


void print(struct students *head)
{
	struct students *p;
	p=head;
	do{
		printf("%5d,%s,%c,%3d\n",p->num,p->name,p->sex,p->score);
		p=p->next;
	}while(p!=NULL);
}

void save(struct students *head)
{
	struct students *p;
	FILE *fp;
	p=head;
	if((fp=fopen("D:\\bcsource\\limk.txt","wt"))==NULL)
	{
		puts("File open error!");
		exit(1);
	}
	do{
		fprintf(fp,"%5d,%s,%c,%3d\n",p->num,p->name,p->sex,p->score);
		p=p->next;
	}while(p!=NULL);
}


void main()
{
	int blnprint;
	struct students *p;
	clrscr();
	p=inputdata();
	puts("Do you want print data [1/0]?");
	scanf("%d",&blnprint);
	if(blnprint>0)	print(p);
	puts("Do you want save data [1/0]?");
	scanf("%d",&blnprint);
	if(blnprint>0) save(p);
}
